      subroutine getmgt(mgtfile_name)

      !! open mgt file based on grazing in scenario design

      use parm

      character (len=*) ::  mgtfile_name
      character (len=Len(mgtfile_name)) :: temp_name
      logical :: alive
      logical :: searcharray

      webs_mgt_is_grazing = .FALSE.
      web_mgt_is_forage = .FALSE.
      web_mgt_is_tillage = .FALSE.
      in_conventioal = .FALSE.


        !!see if current hru is uese to do grazing in
        !!scenario.
        !!If yes, read MGT file in folder grazing_mgt/Grazing_all. If
        !!the file is in that folder, go to main folder
        !!If no, read HRU file in folder grazing_mgt/Grazing_non.If
        !!the file is in that folder, go to main folder
        !!2012-10-16
            if(webs_grazing_hru_num > 0) then
                if(searcharray(webs_grazing_hru_num,webs_grazing_hru,
     &                               ihru)) then
                    temp_name = "grazing_mgt/grazing_all/"//
     &                          mgtfile_name(:len_trim(mgtfile_name))
                else
                    temp_name = "grazing_mgt/grazing_none/"//
     &                          mgtfile_name(:len_trim(mgtfile_name))
                end if
                inquire(file=temp_name,exist=alive)

                if(alive) then
                    mgtfile_name = temp_name
                    webs_mgt_is_grazing = .TRUE.

                    !!debug informatin
                    if(output_test_information) then
                        write(2011,*) mgtfile_name
                    end if

                    return
                else
!                    !!debug informatin
!                    if(output_test_information) then
!                        write(2011,*) "Warning: Can't find " //
!     &                                   temp_name
!                    end if
                end if
            end if

            !!try tillage bmp first, then forage conversion
            !!read different mgt file from different folder based on
            !!base scenario and till type
            !!zhiqiang, 2012-12-17
            if(webs_tillage_hru_num > 0) then
              if(searcharray(webs_tillage_hru_num, webs_tillage_hru,
     &               ihru)) then                  !!selected for tillage
                if(webs_tillage_type == 56) then  !!conventional till
                    if(webs_base == 1) then  !!conventional scenario
                        temp_name="conventional/base/"//
     &                             mgtfile_name(:len_trim(mgtfile_name))
                    else                    !!historical scenario
                        temp_name ="historical/tillage/conventional/"//
     &                             mgtfile_name(:len_trim(mgtfile_name))
                    end if
                end if

                if(webs_tillage_type == 4) then  !!zero till
                    if(webs_base == 1) then  !!conventional
                        temp_name ="conventional/tillage/zero/"//
     &                             mgtfile_name(:len_trim(mgtfile_name))
                    else                !!historical
                        temp_name ="historical/tillage/zero/"//
     &                             mgtfile_name(:len_trim(mgtfile_name))
                    end if
                end if

                if(webs_tillage_type == 56 .OR.
     &               webs_tillage_type == 4) then
                    inquire(file=temp_name,exist=alive)
                    if(alive) then
                        mgtfile_name = temp_name
                        web_mgt_is_tillage = .TRUE.

                        !!debug informatin
                        if(output_test_information) then
                            write(2011,*) mgtfile_name
                        end if

                        return
                    else
                        !!debug informatin
                        if(output_test_information) then
                            write(2011,*) "Warning: Can't find " //
     &                                   temp_name
                        end if
                    end if
                 end if

              end if
            end if

            !!If not selected in grazing, try forage Conversion
            if(webs_forage_hru_num > 0) then
                if(searcharray(webs_forage_hru_num, webs_forage_hru,
     &                               ihru)) then
                    !!if base is different, forage conversion is different
                    !!zhiqiang, 2012-12-17
                    if(webs_base == 1) then  !!conventional
                        temp_name = "conventional/forage/" //
     &                            mgtfile_name(:len_trim(mgtfile_name))
                                        !!use same mgt file
                    else                !!historical
                        temp_name = "historical/forage/" //
     &                            mgtfile_name(:len_trim(mgtfile_name))
                    end if

                    inquire(file=temp_name,exist=alive)
                    if(alive) then
                        mgtfile_name = temp_name
                        web_mgt_is_forage = .TRUE.

                        !!debug informatin
                        if(output_test_information) then
                            write(2011,*) mgtfile_name
                        end if

                        return
                    else
                        !!debug informatin
                        if(output_test_information) then
                            write(2011,*) "Warning: Can't find " //
     &                                   temp_name
                        end if
                    end if
                end if
            end if



      !!try to figure out mgt files of base scenario for scenarios
      !!inherits from conventional scenario.
      if( .NOT.webs_mgt_is_grazing .AND.
     &    .NOT.web_mgt_is_forage .AND.
     &    webs_base == 1) then
         temp_name = "conventional/base/"//
     &            mgtfile_name(:len_trim(mgtfile_name))
         inquire(file=temp_name,exist=alive)
         if(alive) then
            mgtfile_name = temp_name
         else
            !!debug informatin
            if(output_test_information) then
                write(2011,*) "Warning: Can't find " //
     &                                   temp_name
            end if
         end if
      end if

         !!debug information
         if(output_test_information) then
            write(2011,*) mgtfile_name
         end if

          return
      end

